﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.IO;

namespace UIMaker
{
    public class SelectSprite : UITypeEditor
    {
        public static List<SpriteMaker.Sprite> spriteLoaded = new List<SpriteMaker.Sprite>();
        private IWindowsFormsEditorService editorService;
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context == null || context.Instance == null)
                return base.GetEditStyle(context);
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            

            if (context == null || context.Instance == null || provider == null)
                return value;

            try
            {
                // get the editor service, just like in windows forms
                editorService = (IWindowsFormsEditorService)
                   provider.GetService(typeof(IWindowsFormsEditorService));
                ContextMenu menu = new ContextMenu();
                ListBox listSprite = new ListBox();
                listSprite.SelectionMode = SelectionMode.One;
                listSprite.MouseClick += listSprite_MouseClick;
                foreach (SpriteMaker.Sprite s in spriteLoaded)
                {
                    listSprite.Items.Add(s.spriteName);
                }
                listSprite.Items.Add("<Import Sprite>");
                editorService.DropDownControl(listSprite);
                if ((string)listSprite.SelectedItem == "<Import Sprite>")
                {
                    OpenFileDialog dialog = new OpenFileDialog();
                    dialog.Filter = "Sprite (*.sprite)|*.sprite";
                    dialog.CheckFileExists = true;
                    dialog.ShowDialog();
                    if (dialog.FileName != "")
                    {
                        String name = Path.GetFileNameWithoutExtension(dialog.FileName);
                        foreach (SpriteMaker.Sprite s in spriteLoaded)
                        {
                            if (s.spriteName == name)
                            {
                                return name;
                            }
                        }
                        SpriteMaker.Sprite sprite = new SpriteMaker.Sprite();
                        TextReader reader = new StreamReader(dialog.FileName, Encoding.ASCII);
                        String data = reader.ReadToEnd();
                        reader.Close();
                        sprite.FromStringXML(data);
                        sprite.spriteName = name;
                        spriteLoaded.Add(sprite);
                        return name;
                    }
                    else
                    {
                        return value;
                    }
                }
                else
                {
                    if (listSprite.SelectedItem != null)
                    {
                        return listSprite.SelectedItem;
                    }
                    else
                    {
                        return value;
                    }
                }

            }
            finally
            {
                editorService = null;
            }
        }

        void listSprite_MouseClick(object sender, MouseEventArgs e)
        {
            editorService.CloseDropDown();
        }
    }
}
